global !p
def math():
    return vim.eval('vimtex#syntax#in_mathzone()') == '1'

def comment():
    return vim.eval('vimtex#syntax#in_comment()') == '1'

def env(name):
    [x,y] = vim.eval("vimtex#env#is_inside('" + name + "')")
    return x != '0' and x != '0'

endglobal

##############
# MATH STUFF #
##############

snippet beg "begin{} / end{}" bA
\begin{$1}${2}
	$0
\end{$1}
endsnippet

snippet mk "Math" wA
\(${1}`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']:
    snip.rv = ' '
else:
    snip.rv = ''
`$2\)
endsnippet

snippet dm "Math" wA
\[
$1
.\] $0
endsnippet

snippet ali "begin{align} / end{align}" bA
\begin{${1:align}}
	${0}
\end{${1:align}}
endsnippet

snippet '([A-Za-z])(\d)' "auto subscript" wrA
`!p snip.rv = match.group(1)`_`!p snip.rv = match.group(2)`
endsnippet

snippet '([A-Za-z])_(\d\d)' "auto subscript2" wrA
`!p snip.rv = match.group(1)`_{`!p snip.rv = match.group(2)`}
endsnippet

snippet "_([0-9a-zA-Z])" "_{1}" riA
_{${1:`!p snip.rv=match.group(1)`}}$0
endsnippet

snippet "\^(-?.+)'" "^{}" riA
^{${1:`!p snip.rv=match.group(1)`}}$0
endsnippet

snippet "(\w+)('B)" "\mathbb{}" riA
\mathbb{$1}$0
endsnippet

snippet "(\\?\w+)('b)" "\boldsymbol{}" riA
\boldsymbol{`!p snip.rv=match.group(1)`}
endsnippet

snippet fr "\frac{}{}" iA
\frac{${1:${VISUAL}}}{${2:${VISUAL}}}$0
endsnippet

snippet lr[ "\left[\right]" iA
\left[${1:${VISUAL}}\right]$0
endsnippet

snippet lr( "\left(\right)" iA
\left(${1:${VISUAL}}\right)$0
endsnippet

snippet lr{ "\left\{\right\}" iA
\left\{${1:${VISUAL}}\right\}$0
endsnippet

snippet ln "\ln{}" iA
\ln{${1}}${0}
endsnippet

snippet "tr'" "\operatorname{tr}" riA
\operatorname{tr}$0
endsnippet

snippet qu' "\quad" iA
\quad 
endsnippet

snippet qq' "\qquad" iA
\qquad 
endsnippet

snippet exp "\exp \left(\right)" iA
\exp\left(${1}\right)$0
endsnippet

snippet operatorname "\operatorname{}" iA
\operatorname{$1}$0
endsnippet

snippet overset "\overset{}{}" iA
\overset{${1:${VISUAL}}}{$2}$0
endsnippet

snippet underset "\underset{}{}" iA
\underset{$2}{${1:${VISUAL}}}$0
endsnippet

snippet gtrless "\gtrless" iA
\gtrless$0
endsnippet

snippet >= "\ge" iA
 \ge $0
endsnippet

snippet <= "\le" iA
 \le $0
endsnippet

snippet sr "^2" iA
{${1:${VISUAL}}}^{2}
endsnippet

snippet cb "^3" iA
{${1:${VISUAL}}}^{3}
endsnippet

snippet compl "complement" iA
^{c}
endsnippet

snippet td "superscript" iA
^{$1}$0
endsnippet

snippet rs "sqrt root of square" A
\sqrt{$1}$0
endsnippet

priority 1
snippet // "Fraction" iA
\\frac{%
${1:${VISUAL}}
}{%
$2
}$0
endsnippet

priority 10
snippet "bar" "bar" riA
\overline{$1}$0
endsnippet

snippet "hat" "hat" riA
\hat{$1}$0
endsnippet

snippet lr( "braket" iA
\left($1\right)$0
endsnippet

snippet lr{} "curly braket" iA
\left\{$1\right\}$0
endsnippet

snippet lr<> "angle braket" iA
\left\lange $1, $2 \right\rangle$0
endsnippet

snippet "\[(\d|\d+)\]" "cross reference" irA
\cref{${1:eq}:`!p
snip.rv = match.group(1)
`}$0
endsnippet

snippet "\|([^|]+)\|" "absolute value" irA
\left|${1:`!p snip.rv = match.group(1) `}\right|$0
endsnippet

snippet pp "perp" iA
\perp$0
endsnippet

snippet pdv "Partial Derivative" w
\pdv[${1:${VISUAL}}]{${2}}{${3}}
endsnippet

priority 100
snippet || "parallel" iA
\parallel$0
endsnippet

snippet ^. "wedge(land)" iA
\wedge$0
endsnippet

snippet ^| "vee(lor)" iA
\vee$0
endsnippet

snippet ^' "prime" iA
^{'}$0
endsnippet

snippet ^" "double prime" iA
^{''}$0
endsnippet

snippet "\s\.\s" "cdot" riA
\cdot
endsnippet

snippet =\ "neq" iA
\neq $0
endsnippet

snippet sum "sum" iA
\sum_{${2:i=1}}^{${3:N}}{${1:${VISUAL}}}$0
endsnippet

snippet prod "prod" iA
\prod_{${1:i=1}}^{${2:N}}{$3}$0
endsnippet

snippet / "Fraction" iA
\frac{$1}{$2}$0
endsnippet

snippet '((\d+)|(\d*)(\\)?([A-Za-z]+)((\^|_)(\{\d+\}|\d))*)/' "Fraction" wrA
\frac{`!p snip.rv = match.group(1)`}{$1}$0
endsnippet

snippet "([a-zA-Z])bar" "bar" riA
\overline{`!p snip.rv=match.group(1)`}
endsnippet

snippet "([a-zA-Z])hat" "hat" riA
\hat{`!p snip.rv=match.group(1)`}
endsnippet

snippet "(\\?\w+)(,\.|\.,)" "Vector postfix" riA
\mathbf{`!p snip.rv=match.group(1)`}
endsnippet

snippet eq "Equation witha number" b
\begin{equation}\label{eq:${1}}
	${0:${VISUAL}}
\end{equation}
endsnippet

snippet eqnn "Equation without a number" b
\begin{equation*}
	${0:${VISUAL}}
\end{equation*}
endsnippet

snippet al "Align" b
\begin{align}\label{eq:${1}}
	${0:${VISUAL}}
\end{align}
endsnippet

snippet alnn "Align without a number" b
\begin{align*}
	${0:${VISUAL}}
\end{align*}
endsnippet

snippet ald "Aligned" b
\begin{aligned}\label{eq:${1}}
	${0:${VISUAL}}
\end{aligned}
endsnippet

snippet aldnn "Aligned without a number" b
\begin{aligned*}
	${0:${VISUAL}}
\end{aligned*}
endsnippet

snippet array "array" bw
\begin{array}{${0:c}}
	${1:${VISUAL}}
\end{array}
endsnippet

snippet eqa "Equation array" b
\begin{eqnarray}
	${1:${VISUAL}} & ${2:${VISUAL}} & ${0:${VISUAL}}
\end{eqnarray}
endsnippet

snippet eqann "Equation array without a number" b
\begin{eqnarray*}
	${1:${VISUAL}} & ${2:${VISUAL}} & ${0:${VISUAL}}
\end{eqnarray*}
endsnippet

snippet lb "Equation number" A
\label{eq:${0:${VISUAL}}}
endsnippet

snippet int "Integration" A
\int_{${3}}^{${4}}{${2:${VISUAL}}d{${1:t}}}${0}
endsnippet

snippet part "Partial Derivative" A
\partial $0
endsnippet

snippet dd "Derivative" A
\frac{d${1:s}}{d${2:t}}$0
endsnippet

snippet -> "right arrow" A
\rightarrow
endsnippet



##################
#  Greek letters  #
##################

snippet "([a-zA-Z])`" "Greek Letter " riA
`!p
greek_alphabet = {
    u'\u0391': 'Alpha',
    u'\u0392': 'Beta',
    u'\u0393': 'Gamma',
    u'\u0394': 'Delta',
    u'\u0395': 'Epsilon',
    u'\u0396': 'Zeta',
    u'\u0397': 'Eta',
    u'\u0398': 'Theta',
    u'\u0399': 'Iota',
    u'\u039A': 'Kappa',
    u'\u039B': 'Lambda',
    u'\u039C': 'Mu',
    u'\u039D': 'Nu',
    u'\u039E': 'Xi',
    u'\u039F': 'Omicron',
    u'\u03A0': 'Pi',
    u'\u03A1': 'Rho',
    u'\u03A3': 'Sigma',
    u'\u03A4': 'Tau',
    u'\u03A5': 'Upsilon',
    u'\u03A6': 'Phi',
    u'\u03A7': 'Chi',
    u'\u03A8': 'Psi',
    u'\u03A9': 'Omega',
    u'\u03B1': 'alpha',
    u'\u03B2': 'beta',
    u'\u03B3': 'gamma',
    u'\u03B4': 'delta',
    u'\u03B5': 'epsilon',
    u'\u03B6': 'zeta',
    u'\u03B7': 'eta',
    u'\u03B8': 'theta',
    u'\u03B9': 'iota',
    u'\u03BA': 'kappa',
    u'\u03BB': 'lamda',
    u'\u03BC': 'mu',
    u'\u03BD': 'nu',
    u'\u03BE': 'xi',
    u'\u03BF': 'omicron',
    u'\u03C0': 'pi',
    u'\u03C1': 'rho',
    u'\u03C3': 'sigma',
    u'\u03C4': 'tau',
    u'\u03C5': 'upsilon',
    u'\u03C6': 'phi',
    u'\u03C7': 'chi',
    u'\u03C8': 'psi',
    u'\u03C9': 'omega',
}
greek_dict = {
    'A': 'Alpha',
    'B': 'Beta',
    'G': 'Gamma',
    'D': 'Delta',
    'E': 'Epsilon',
    'Z': 'Zeta',
    'H': 'Eta',
    'Q': 'Theta',
    'I': 'Iota',
    'K': 'Kappa',
    'L': 'Lambda',
    'M': 'Mu',
    'N': 'Nu',
    'X': 'Xi',
    'O': 'Omicron',
    'P': 'Pi',
    'R': 'Rho',
    'S': 'Sigma',
    'T': 'Tau',
    'U': 'Upsilon',
    'F': 'Phi',
    'C': 'Chi',
    'J': 'Psi',
    'W': 'Omega',
    'a': 'alpha',
    'b': 'beta',
    'g': 'gamma',
    'd': 'delta',
    'e': 'epsilon',
    'z': 'zeta',
    'h': 'eta',
    'q': 'theta',
    'i': 'iota',
    'k': 'kappa',
    'l': 'lamda',
    'm': 'mu',
    'n': 'nu',
    'x': 'xi',
    'o': 'omicron',
    'p': 'pi',
    'r': 'rho',
    's': 'sigma',
    't': 'tau',
    'u': 'upsilon',
    'f': 'phi',
    'c': 'chi',
    'j': 'psi',
    'w': 'omega',
}
key = match.group(1)
snip.rv = f'\\{greek_dict[key[0]]}'`
endsnippet

#################
# PHYSICS STUFF #
#################

snippet "\\\?cos"  "cosine function"  rA
\cos{${1}}${0}
endsnippet

snippet "\\\?sin"  "sine function"  rA
\sin{${1}}${0}
endsnippet

snippet pdv "Partial Derivative" w
\frac{\partial{${1:${VISUAL}}}}{\partial{${2}}}
endsnippet

snippet dv "Derivative" w
\frac{d${1:${VISUAL}}}{d${2:x}}${0}
endsnippet

snippet "\(" "smart paren" riA
(${1})${0}
endsnippet

snippet \{ "smart paren" riA
{${1}}${0}
endsnippet

snippet [] "smart paren" iA
[${1}]${0}
endsnippet

snippet \* "times" iA
\times
endsnippet

snippet SI "SI-Unit" w
\SI{${1:${VISUAL}}}{${2}}
endsnippet

priority 1000
snippet '^.*\)/' "() Fraction" wrA
`!p
stripped = match.string[:-1]
depth = 0
i = len(stripped) - 1
while True:
    if stripped[i] == ')': depth += 1
    if stripped[i] == '(': depth -= 1
    if depth == 0: break;
    i -= 1
snip.rv = stripped[0:i] + "\\frac{" + stripped[i+1:-1] + "}"
`{$1}$0
endsnippet

snippet sympy "sympy block " w
sympy $1 sympy$0
endsnippet

priority 10000

snippet Ex "Expectation" w
\mathbb{E}\left[${1}\right]${0}
endsnippet


snippet 'sympy(.*)sympy' "evaluate sympy" wr
`!p
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
init_printing()
snip.rv = eval('latex(' + match.group(1).replace('\\', '') \
    .replace('^', '**') \
    .replace('{', '(') \
    .replace('}', ')') + ')')
`
endsnippet

# vim:ft=snippets:
